/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.vertexnet.realestate.view.panel;

import static com.vertexnet.realestate.constants.UIConstants.AGENT;
import static com.vertexnet.realestate.constants.UIConstants.CUSTOMER;
import static com.vertexnet.realestate.constants.UIConstants.EMPLOYEE;
import static com.vertexnet.realestate.constants.UIConstants.LAYOUT;
import static com.vertexnet.realestate.constants.UIConstants.PAYMENT_PLAN;

import java.util.List;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import com.vertexnet.realestate.view.utility.DefaultTable;

import net.coderazzi.filters.gui.TableFilterHeader;

import com.vertexnet.realestate.interfaces.Controller;
import com.vertexnet.realestate.interfaces.DefaultPanel;
import com.vertexnet.realestate.interfaces.MainView;
import com.vertexnet.realestate.interfaces.TableModel;
import com.vertexnet.realestate.model.tablemodel.CustomersTableModel;
import com.vertexnet.realestatenet.model.bean.Agent;
import com.vertexnet.realestatenet.model.bean.Customer;
import com.vertexnet.realestatenet.model.bean.Employee;
import com.vertexnet.realestatenet.model.bean.Layout;
import com.vertexnet.realestatenet.model.bean.PaymentPlan;

/**
 *
 * @author Srivathsa
 */
public class DefaultPanelImpl extends javax.swing.JPanel implements DefaultPanel {

	private static final long serialVersionUID = 1L;
	private TableModel tableModel;
	private Controller controller;
	private MainView view;
	TableFilterHeader headerRenderer;

	/**
     * Creates new form DefaultRealEstatePanel
     * @param model 
	 * @param controller 
     */
    public DefaultPanelImpl(TableModel tableModel, Controller controller, MainView view) {
    	this.tableModel = tableModel;
    	this.controller = controller;
    	this.view = view;
        initComponents();
        headerRenderer = new TableFilterHeader(table);
        headerRenderer.setVisible(false);
		//table.getColumnModel().getColumn(0).setMaxWidth(45);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        logoLabel = new javax.swing.JLabel();
        reportButton = new javax.swing.JButton();
        addButton = new javax.swing.JButton();
        editButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        filterButton = new javax.swing.JToggleButton();
        tableScrollPane = new javax.swing.JScrollPane();
        table = new com.vertexnet.realestate.view.utility.DefaultTable();
        makePaymentButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        logoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/vertexnet/realestate/view/icons/logo.gif"))); // NOI18N

        reportButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/vertexnet/realestate/view/icons/report.png"))); // NOI18N
        reportButton.setText("Generate Report");
        reportButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                reportButtonActionPerformed(evt);
            }
        });

        addButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/vertexnet/realestate/view/icons/add.png"))); // NOI18N
        addButton.setText("Add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });

        editButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/vertexnet/realestate/view/icons/edit.png"))); // NOI18N
        editButton.setText("Edit");
        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        deleteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/vertexnet/realestate/view/icons/delete.png"))); // NOI18N
        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        filterButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/vertexnet/realestate/view/icons/filter.png"))); // NOI18N
        filterButton.setText("Filter");
        filterButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                filterButtonActionPerformed(evt);
            }
        });

        table.setModel((javax.swing.table.TableModel) tableModel);
        tableScrollPane.setViewportView(table);

        makePaymentButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/vertexnet/realestate/view/icons/payment.png"))); // NOI18N
        makePaymentButton.setText("Make Payment");
        makePaymentButton.setName(CUSTOMER.getMakePayment());
        makePaymentButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                makePaymentButtonActionPerformed(evt);
            }
        });

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/vertexnet/realestate/view/icons/nimsoft_logo.png"))); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(tableScrollPane)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(logoLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 6, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(makePaymentButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(reportButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(addButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(editButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(deleteButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(filterButton, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 272, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(logoLabel)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(deleteButton)
                            .addComponent(editButton)
                            .addComponent(addButton, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(reportButton)
                            .addComponent(filterButton, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(makePaymentButton))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tableScrollPane, javax.swing.GroupLayout.DEFAULT_SIZE, 301, Short.MAX_VALUE)
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {addButton, deleteButton, editButton, filterButton, reportButton});

    }// </editor-fold>//GEN-END:initComponents

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
    	controller.handleEvent(evt);
    }//GEN-LAST:event_addButtonActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
    	getSelectedObjects();
    	controller.handleEvent(evt);
    	if (table.getName().equals(CUSTOMER.getEdit()) && !view.getSelectedCustomers().isEmpty()) {
    		((CustomersTableModel)tableModel).reloadTotalData(view.getSelectedCustomers().get(0));
    	}
    }//GEN-LAST:event_editButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
    	getSelectedObjects();
    	Integer value = JOptionPane.showConfirmDialog(this, "Are you Sure you want to delete selected rows", "Confirmation", JOptionPane.YES_NO_OPTION);
    	if(value.equals(JOptionPane.YES_OPTION)) {
        	controller.handleEvent(evt);
    	}
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void reportButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_reportButtonActionPerformed
    	getSelectedObjects();
    	controller.handleEvent(evt);
    }//GEN-LAST:event_reportButtonActionPerformed

    private void filterButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_filterButtonActionPerformed
    	boolean selected = filterButton.getModel().isSelected();
    	headerRenderer.setVisible(selected);	
    	headerRenderer.setEnabled(selected);
    }//GEN-LAST:event_filterButtonActionPerformed

    private void makePaymentButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_makePaymentButtonActionPerformed
    	getSelectedObjects();
    	controller.handleEvent(evt);
    	if (table.getName().equals(CUSTOMER.getEdit()) && !view.getSelectedCustomers().isEmpty()) {
    		((CustomersTableModel)tableModel).reloadTotalData(view.getSelectedCustomers().get(0));
    	}
    }//GEN-LAST:event_makePaymentButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton editButton;
    private javax.swing.JToggleButton filterButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel logoLabel;
    private javax.swing.JButton makePaymentButton;
    private javax.swing.JButton reportButton;
    private com.vertexnet.realestate.view.utility.DefaultTable table;
    private javax.swing.JScrollPane tableScrollPane;
    // End of variables declaration//GEN-END:variables
    

	public JButton getReportButton() {
		return reportButton;
	}
    
	public JButton getAddButton() {
		return addButton;
	}

	public JButton getEditButton() {
		return editButton;
	}

	public JButton getDeleteButton() {
		return deleteButton;
	}

	public DefaultTable getTable() {
		return table;
	}
	
	@SuppressWarnings("unchecked")
	public void getSelectedObjects() {
		List<?> objects = tableModel.getSelectedObjects();
		if (table.getName().equals(CUSTOMER.getEdit())) {
			view.setSelectedCustomers((List<Customer>) objects);
		} else if (table.getName().equals(LAYOUT.getEdit())) {
			view.setSelectedLayouts((List<Layout>) objects);
		} else if (table.getName().equals(PAYMENT_PLAN.getEdit())) {
			view.setSelectedPaymentPlans((List<PaymentPlan>) objects);
		} else if (table.getName().equals(AGENT.getEdit())) {
			view.setSelectedAgents((List<Agent>) objects);
		} else if (table.getName().equals(EMPLOYEE.getEdit())) {
			view.setSelectedEmployees((List<Employee>) objects);
		}
	}

	public JButton getMakePaymentButton() {
		return makePaymentButton;
	}
}
